﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;


namespace CMS.BLL
{
    public class City
    {
        #region Common
        public eCity Data;

        public City()
        {
            Data = new eCity();
		}

        ~City()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cCity objDAL = new cCity();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cCity objDAL = new cCity();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cCity objInfor = new cCity(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.CityID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }
        //Update nhung cai ma eCity Data dang nam giu thong qua cCity objInfor
        public int Update(int ID)
        {
            int nResult = 0;
            cCity objInfor = new cCity();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        } 
        private void MappingData(cCity objInfor)
        {
            try { objInfor.CityName1 = Data.CityName1; }
            catch { objInfor.CityName1 = ""; }
            try { objInfor.CityName2 = Data.CityName2; }
            catch { objInfor.CityName2 = ""; }
            try { objInfor.CityName3 = Data.CityName3; }
            catch { objInfor.CityName3 = ""; }

            objInfor.RegionID = Data.RegionID;
        }
        private void ConvertData(cCity objInfor)
        {
            try { Data.CityName1 = objInfor.CityName1; }
            catch { Data.CityName1 = ""; }
            try { Data.CityName2 = objInfor.CityName2; }
            catch { Data.CityName2 = ""; }
            try { Data.CityName3 = objInfor.CityName3; }
            catch { Data.CityName3 = ""; }

            Data.RegionID = objInfor.RegionID;
        }
        public DataTable GetALL()
        {
            DataTable tbResult = new DataTable();
            cCity objDAL = new cCity();
            tbResult = objDAL.GetALL();
            return tbResult;
        }
        public DataTable SearchByRegion(int Region)
        {
            DataTable tbResult = new DataTable();
            cCity objDAL = new cCity();
            tbResult = objDAL.SearchByRegion(Region);
            return tbResult;
        }
        public DataTable SearchByCountry(int Country)
        {
            DataTable tbResult = new DataTable();
            cCity objDAL = new cCity();
            tbResult = objDAL.SearchByCountry(Country);
            return tbResult;
        }

        public DataTable GetCityByCountryID(int CountryID)
        {
            DataTable tbResult = new DataTable();
            cCity objDAL = new cCity();
            tbResult = objDAL.GetCityByCountryID(CountryID);
            return tbResult;
        }
       #endregion
    }
}
